package com.hst.capacity.infra.persistence.jpa.setting.price;

import com.hst.capacity.domain.model.entity.setting.price.AirportCommonPriceEntity;
import com.hst.capacity.domain.model.entity.setting.price.StationCommonPriceEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface StationCommonPriceRepository extends JpaRepository<StationCommonPriceEntity, String> {

    @Query("select t from StationCommonPriceEntity t where t.idFranchisee=:idFranchisee")
    Page<StationCommonPriceEntity> findByIdFranchisee(String idFranchisee, PageRequest pageRequest);

    /**
     * 通过地区查找站点价格
     * @param regionCode
     * @return
     */
    @Query("select o " +
            "from StationCommonPriceEntity o " +
            "inner join FranchiseeEntity a on a.idFranchisee = o.idFranchisee " +
            "where a.regionCode =:regionCode ")
    List<StationCommonPriceEntity> findByRegionCode(@Param("regionCode")String regionCode);

    /**
     * 通过地区查找站点价格
     * @param idStation
     * @return
     */
    @Query("select o " +
            "from StationCommonPriceEntity o " +
            "where o.idFranchisee =:idFranchisee " +
            "and o.idStation =:idStation " +
            "and o.idServiceType =:idServiceType " +
            "and o.idVehicleType =:idVehicleType " )
    StationCommonPriceEntity findByIdStation(@Param("idFranchisee")String idFranchisee, @Param("idStation")String idStation, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType);

    /**
     * 通过地区查找站点价格
     * @param idStation
     * @return
     */
    @Query("select t " +
            "from StationCommonPriceEntity t " +
            "where t.idFranchisee =:idFranchisee " +
            "and t.idStation =:idStation " +
            "and t.idServiceType =:idServiceType " +
            "and t.idVehicleType =:idVehicleType " +
            "and t.channel =:channel " )
    StationCommonPriceEntity findAllByIdStation(@Param("idFranchisee")String idFranchisee, @Param("idStation")String idStation, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idStation
     * @return
     */
    @Query("select t " +
            "from StationCommonPriceEntity t " +
            "where t.idFranchisee =:idFranchisee " +
            "and t.idStation =:idStation " +
            "and t.idServiceType =:idServiceType " +
            "and t.idVehicleType =:idVehicleType " +
            "and t.channel =:channel " +
            "and t.status = 1" )
    StationCommonPriceEntity findByIdStationAndStatusOnline(@Param("idFranchisee")String idFranchisee, @Param("idStation")String idStation, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idStation
     * @return
     */
    @Query("select t " +
            "from StationCommonPriceEntity t " +
            "where t.idFranchisee =:idFranchisee " +
            "and t.idStation =:idStation " +
            "and t.idServiceType =:idServiceType " +
            "and t.idVehicleType =:idVehicleType " +
            "and t.channel =:channel ")
    StationCommonPriceEntity findByIdStation(@Param("idFranchisee")String idFranchisee, @Param("idStation")String idStation, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idStation
     * @return
     */
    @Query("select o " +
            "from StationCommonPriceEntity o " +
            "where o.idFranchisee =:idFranchisee " +
            "and o.idStation =:idStation " +
            "and o.idServiceType =:idServiceType " +
            "and o.status = 1")
    List<StationCommonPriceEntity> findByIdStation(@Param("idFranchisee")String idFranchisee, @Param("idStation")String idStation, @Param("idServiceType")int idServiceType);

    /**
     * 通过地区查找站点价格
     * @param idPrice
     * @return
     */
    @Query("select t " +
            "from StationCommonPriceEntity t " +
            "where t.idServicePriceType = :idPrice")
    StationCommonPriceEntity findMarkupByIdServicePriceType(@Param("idPrice") String idPrice);

}